﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericEntities;
using System.Collections;

namespace DiaryPlanner
{
    public partial class PlannerDay : UserControl
    {
        Planner planner;

        List<Label> dayList = new List<Label>();
        List<Label> timeList = new List<Label>();

        const int week = 7;
        const int time = 24;
        const int half = 2;
        const int minute = 30;
        const int labelNum = time * half;

        System.Drawing.Color userColor = System.Drawing.Color.SkyBlue;
        System.Drawing.Color userColor1 = System.Drawing.Color.GreenYellow;

        public PlannerDay()
        {
            InitializeComponent();
            skinSet();
            initDay();
        }
        public void skinSet()
        {
            string skin = UserProfile.Instance().setting;
            if (skin == "b")
            {
                this.BackColor = System.Drawing.Color.PaleTurquoise;

            }
            else if (skin == "c")
            {
                this.BackColor = System.Drawing.Color.LawnGreen;
            }
            else if (skin == "d")
            {
                this.BackColor = System.Drawing.Color.MistyRose;

            }
            else
            { //skin=="a"
                this.BackColor = System.Drawing.SystemColors.ControlLight;

            }
        }
        private void initDay()
        {
            CheckForIllegalCrossThreadCalls = false; //스레드 충돌에러 없애기

            int i;

            for (i = 0; i < labelNum; i++)
                dayList.Add(new System.Windows.Forms.Label());

            for (i = 0; i < labelNum; i++)
                timeList.Add(new System.Windows.Forms.Label());

            this.SuspendLayout();

            for (i = 0; i < labelNum; i++)
            {
                dayList[i].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                dayList[i].Location = new System.Drawing.Point(0, 0);
                dayList[i].Margin = new System.Windows.Forms.Padding(0);
                dayList[i].Name = "labelD" + i.ToString();
                dayList[i].Size = new System.Drawing.Size(54*week, 25);
                dayList[i].BackColor = System.Drawing.Color.White;
                dayList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                timeList[i].Location = new System.Drawing.Point(0, 0);
                timeList[i].Margin = new System.Windows.Forms.Padding(0);
                timeList[i].Name = "labelT" + i.ToString();
                timeList[i].Size = new System.Drawing.Size(53, 25);
                if (i % 2 == 0) timeList[i].Text = getTime_KR(i / 2);
                else timeList[i].Text = "";
            }

            for (i = 0; i < labelNum; i++)
            {
                this.flowLayoutPanel1.Controls.Add(timeList[i]);
                this.flowLayoutPanel1.Controls.Add(dayList[i]);
            }

            this.ResumeLayout(false);
            this.PerformLayout();

            //클릭 이벤트 핸들러 등록
            for (i = 0; i < dayList.Count; i++)
                dayList[i].Click += new System.EventHandler(this.dayList_Click);

            //더블 클릭 이벤트 핸들러 등록
            for (i = 0; i < dayList.Count; i++)
                dayList[i].DoubleClick += new System.EventHandler(this.dayList_DoubleClick);
        }

        public void viewDay()
        {
            for (int i = 0; i < labelNum; i++)
            {
                dayList[i].Text = "";
            }
        }

        public void viewAllDB()
        {
            viewDay();

            IList list = CalendarDB.Instance().loadAll_db();

            planner.plannerListClear();

            int select = 0;

            for (int i = 0; i < dayList.Count; i++)
            {
                if (dayList[i].BackColor == userColor)
                {
                    select = i;
                }
                else if (dayList[i].BackColor == userColor1)
                    dayList[i].BackColor = System.Drawing.Color.White;
               
            }
            
            for (int i = 0; i < list.Count; i++)
            {
                if (planner.monthCalendar1.SelectionStart.Year.ToString() == ((GenericEvent)list[i]).StartTime.Year.ToString() &&
                planner.monthCalendar1.SelectionStart.Month.ToString() == ((GenericEvent)list[i]).StartTime.Month.ToString() &&
                planner.monthCalendar1.SelectionStart.Day.ToString() == ((GenericEvent)list[i]).StartTime.Day.ToString())
                {
                    dayList[int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * half
                        + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute)].Text = ((GenericEvent)list[i]).Title;
                    if (dayList[int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * half
                        + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute)].BackColor == System.Drawing.Color.White)
                        dayList[int.Parse(((GenericEvent)list[i]).StartTime.Hour.ToString()) * half
                            + (int.Parse(((GenericEvent)list[i]).StartTime.Minute.ToString()) / minute)].BackColor = userColor1;

                    planner.plannerListAdd((GenericEvent)list[i]);
                }
            }
        }

        private string getTime_KR(int time)
        {
            string temp = "";

            if (time == 0)
                temp = "오전12시";
            else if (time < 12)
            {
                temp = "오전" + (time).ToString() + "시";
            }
            else if (time == 12)
                temp = "오후12시";
            else
            {
                temp = "오후" + (time - 12).ToString() + "시";
            }
            return temp;
        }

        private string getTime_colon(int time)
        {
            string temp;

            if (time == 0) temp = "00";
            else temp = (time / 2).ToString();

            if (time % 2 == 0) temp = temp + ":" + ((time % 2) * 30).ToString()+"0";
            else temp = temp + ":" + ((time % 2)*30).ToString();

            return temp;
        }

        private void dayList_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;

            for (int i = 0; i < dayList.Count; i++)
                if(dayList[i].BackColor == userColor)
                dayList[i].BackColor = System.Drawing.Color.White;

            label.BackColor = userColor;

            viewAllDB();
        }

        private void dayList_DoubleClick(object sender, EventArgs e)
        {
            PlannerEdit PlannerEditObject = new PlannerEdit();
            PlannerEditObject.initPlannerEdit(planner, this);
            PlannerEditObject.ShowDialog();
            viewAllDB();
        }

        public string dayTime(int time)
        {
            for (int i = 0; i < dayList.Count; i++)
            {
                if (dayList[i].BackColor == userColor)
                {
                    if (time == 0) return getTime_colon(i);
                    else return getTime_colon(i + 1);
                }
            }
            return null;
        }

        public void initPlanner(Planner planner)
        {
            this.planner = planner;
            viewAllDB();
        }

        public void setUnSelected()
        {
            for (int i = 0; i < dayList.Count; i++)
                if (dayList[i].BackColor == userColor)
                dayList[i].BackColor = System.Drawing.Color.White;
        }
    }
}
